package org.com.app.knock.fragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.com.app.knock.GcmBroadcastReceiver;
import org.com.app.knock.KnockApplication;
import org.com.app.knock.activity.FriendActivity;
import org.com.app.knock.view.LoadingView;
import org.json.JSONArray;
import org.json.JSONObject;

import z.lib.base.BaseItem;
import z.lib.base.BaseServiceCallBack;
import z.lib.base.BaseUtils;
import z.lib.base.BaseUtils.API;
import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.callback.RestClient.RequestMethod;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.acv.libs.nbase.BaseFragment;
import com.knock.app.R;

public class LoginFragment extends BaseFragment implements AnimationListener {
	protected static final String TAG = "LoginFragment";
	private View views;
	private LoadingView loading;
	public LoginFragment() {
		super();
	}

	@Override
	public void setUpFragment(View view) {
		this.views = view;
		CommonAndroid.getView(view, R.id.btn_back).setOnClickListener(this);
		CommonAndroid.getView(view, R.id.login).setOnClickListener(this);
		loading = CommonAndroid.getView(view, R.id.loading);
	}

	@Override
	public int layoytResurce() {
		return R.layout.login;
	}
	
	@Override
	public void onClick(View v) {
		super.onClick(v);
		int idView = v.getId();
		switch (idView) {
		case R.id.btn_back:
			getActivity().finish();
			break;
		case R.id.login:
//			String name = CommonAndroid.getView(views, R.id.btn_back);
			TextView login_value1 = CommonAndroid.getView(views, R.id.login_name);
			TextView login_value2 = CommonAndroid.getView(views, R.id.login_pwd);
			String user_name 	= login_value1.getText().toString().trim();
			String pwd 			= login_value2.getText().toString().trim();
			if(!"".equals(user_name) && !"".equals(pwd)){
//				GcmBroadcastReceiver.loginDevice(getActivity(), user_name);
				logIn(user_name, pwd);
			}else{
				BaseUtils.showDialog(getActivity(), "user name not blank!" );
			}
			InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.hideSoftInputFromWindow(v.getWindowToken(), 0);
			break;
		default:
			break;
		}
	}

	private void logIn(String user_name, String pwd) {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("name", user_name);
		params.put("password", pwd);

		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_USER_LOGIN, params, callback);
	}
	
	private BaseServiceCallBack callback = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
//			CommonAndroid.showView(true, getView().findViewById(R.id.progressbar));
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
//				BaseUtils.showDialog(getActivity(), "" + message);
//				CommonAndroid.showView(false, getView().findViewById(R.id.progressbar));
//				Intent Page = new Intent(getActivity(), FriendActivity.class);
//				getActivity().startActivityForResult(Page,0); 
				LogUtils.e(TAG,"err=="+message);
				BaseUtils.showDialog(getActivity(), "" + message);
			}
		};

		public void onSucces(String response) {
//			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				JSONObject mainJsonObject;
				try {
					mainJsonObject = new JSONObject(response);
					String is_succes = CommonAndroid.getString(mainJsonObject,
							BaseUtils.KEY.is_success);
					LogUtils.e(TAG,"done"+ mainJsonObject.toString());
//					String err_msg = CommonAndroid.getString(mainJsonObject,
//							BaseUtils.KEY.err_msg);
//					if (BaseUtils.VALUE.STATUS_API_SUCCESS.equals(is_succes)) {
//						BaseUtils.showDialog(getActivity(), err_msg );
//					}else{
//						BaseUtils.showDialog(getActivity(), err_msg );
//					}
					addDeviceToken();
//					Intent Page = new Intent(getActivity(), FriendActivity.class);
//					getActivity().startActivityForResult(Page,0); 
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
	};

	@Override
	public void onAnimationStart(Animation animation) {

	}

	protected void addDeviceToken() {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		params.put("platform", "1");
		params.put("devicetoken", KnockApplication.token_device_gcm);

		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_USER_DEVICE, params, callbackdevice);
	}
	
	private BaseServiceCallBack callbackdevice = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
//			CommonAndroid.showView(true, getView().findViewById(R.id.progressbar));
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
//				BaseUtils.showDialog(getActivity(), "" + message);
//				CommonAndroid.showView(false, getView().findViewById(R.id.progressbar));
//				Intent Page = new Intent(getActivity(), FriendActivity.class);
//				getActivity().startActivityForResult(Page,0); 
				LogUtils.e(TAG,"err=="+message);
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				JSONObject mainJsonObject;
				try {
					/*mainJsonObject = new JSONObject(response);
					String is_succes = CommonAndroid.getString(mainJsonObject,
							BaseUtils.KEY.is_success);
					LogUtils.e(TAG,"done device"+ mainJsonObject.toString());
					Intent Page = new Intent(getActivity(), FriendActivity.class);
					getActivity().startActivityForResult(Page,0); */
					onLoadIcon();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		};
	};
	
	private void onLoadIcon() {
		// TODO Auto-generated method stub
		HashMap<String, String> params = new HashMap<String, String>();
		BaseUtils.execute(getActivity(), RequestMethod.POST, API.API_DATA, params, callbackget);
	}
	
	private BaseServiceCallBack callbackget = new BaseServiceCallBack() {

		public void onStart() {
			CommonAndroid.showView(true, loading);
			CommonAndroid.hiddenKeyBoard(getActivity());
		};

		public void onError(String message) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				LogUtils.e(TAG,"err=="+message);
				goFriend();
			}
		};

		public void onSucces(String response) {
			CommonAndroid.showView(false, loading);
			if (!isFinish()) {
				goFriend();
			}
		};
	};
	
	private void goFriend(){
		Intent Page = new Intent(getActivity(), FriendActivity.class);
		getActivity().startActivityForResult(Page,0); 
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		mHandle.sendEmptyMessage(0);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {

	}
}